package com.hzeport.bgs22emduty.config.login;

import com.hzeport.bgs22emduty.dto.CustomCodeListDTO;
import com.hzeport.bgs22emduty.entity.SystemModule;
import com.hzeport.bgs22emduty.outbound.SystemModuleRepository;
import com.hzeport.bgs22emduty.vo.SystemModule.SystemModuleVO;
import com.hzeport.bgs22emduty.auth.adapter.UserService;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
@Profile("bgs22emduty")
public class LoginTestCompoent implements LoginCompoentService {

    @Autowired
    private UserService userService;
    @Autowired
    private SystemModuleRepository systemModuleRepository;

    public UserInfoDetailVO getUserInfo(){
//        UserInfoDetailVO userInfoDetailVO = new UserInfoDetailVO();
//        String ticket= ServletAttributes.getRequest().getHeader(H4AEnum.HEAD_TICKET.getValue());
//        try {
//            if (ticket!=null){
//                User user=userService.getUsers(ticket);
//                userInfoDetailVO.setName(user.getFullPath());
//                userInfoDetailVO.setUserName(user.getName());
//                userInfoDetailVO.setCustomsCode(user.getCustomscode());
//                userInfoDetailVO.setUserId(user.getHrcode());
//                userInfoDetailVO.setPersonId(user.getHrcode());
//            }else{
//                throw new Exception("获取用户信息异常");
//            }
//        }catch (Exception e) {
//            log.error("获取用户信息异常"+e);
//        }
//        return userInfoDetailVO;
        //todo 测试数据，3538350
//        return new UserInfoDetailVO().setUserId("2905230").setUserName("朱某某").setCustomscode("ff27946f-5536-46e2-8c17-e09e6adaa209").setPersonId("2981990").setRole(Arrays.asList("123","MANAGER")).setCustomsName("重庆海关");
//        return new UserInfoDetailVO().setUserId("3538350").setUserName("张飞").setCustomscode("b017dbfe-e9a5-4ba2-86e0-fb543372c882").setPersonId("2981990").setRole(Arrays.asList("123","MANAGER")).setCustomsName("广东分署");
//        return new UserInfoDetailVO().setUserId("3538350").setUserName("张飞").setCustomscode("94480300-fca2-4268-86c2-4c13963bf4cb").setPersonId("2981990").setRole(Arrays.asList("123","MANAGER")).setCustomsName("上海海关");
        return new UserInfoDetailVO().setUserId("3538350").setUserName("张飞").setCustomscode("94480300-fca2-4268-86c2-4c13963bf4cb").setPersonId("3538350").setRole(Arrays.asList("123","ZGGLY","MANAGER")).setCustomsName("海关总署");
    }

    @Override
    public Object systemModule() {
        //菜单树集合
        List<SystemModuleVO> systemModuleVOList = new ArrayList<>();
//        全部菜单
        List<SystemModule> List = systemModuleRepository.list();
        List<String> functionCodes = new ArrayList<>();
        //菜单树为空
        if(!CollectionUtils.isEmpty(functionCodes)){
            //在全部菜单中找到每个人对应功能点菜单
            List<SystemModule> moduleList=new ArrayList<>();
            for (String functionCode : functionCodes) {
                moduleList=List.stream().filter(i -> i.getFauthorityid().equals(functionCode)).collect(Collectors.toList());
            }
            //菜单排序
            List<SystemModule> moduleLists=moduleList.stream().sorted(Comparator.comparing(SystemModule::getForderindex)).collect(Collectors.toList());
            //        找到一级菜单
            for (SystemModule systemModule : moduleLists) {
                if(systemModule.getMLevel()==1){
                    //根据一级菜单找到二级
                    List<SystemModuleVO> Children=new ArrayList<>();  //二级菜单集合
                    getChild(systemModule,Children,List);//递归找到二级菜单
                    //二级菜单排序
                    List<SystemModuleVO> Childrens=Children.stream().sorted(Comparator.comparing(SystemModuleVO::getForderindex)).collect(Collectors.toList());
                    SystemModuleVO systemModuleVO = new SystemModuleVO();//菜单树对象
                    systemModuleVO.setId(systemModule.getId());
                    systemModuleVO.setLabel(systemModule.getFmainalias());
                    systemModuleVO.setPath(systemModule.getWPath());
                    systemModuleVO.setChildren(Childrens);
                    //添加到集合
                    systemModuleVOList.add(systemModuleVO);
                }
            }
        }else {
            List<SystemModule> moduleLists=List.stream().sorted(Comparator.comparing(SystemModule::getForderindex)).collect(Collectors.toList());
            //        找到一级菜单
            for (SystemModule systemModule : moduleLists) {
                if("2000".equals(systemModule.getId())){
                    //根据一级菜单找到二级
                    List<SystemModuleVO> Children=new ArrayList<>();  //二级菜单集合
                    getChild(systemModule,Children,List);//递归找到二级菜单
                    //二级菜单排序
                    List<SystemModuleVO> Childrens=Children.stream().sorted(Comparator.comparing(SystemModuleVO::getForderindex)).collect(Collectors.toList());
                    SystemModuleVO systemModuleVO = new SystemModuleVO();//菜单树对象
                    systemModuleVO.setId(systemModule.getId());
                    systemModuleVO.setLabel(systemModule.getFmainalias());
                    systemModuleVO.setPath(systemModule.getWPath());
                    systemModuleVO.setChildren(Childrens);
                    //添加到集合
                    systemModuleVOList.add(systemModuleVO);
                }
                if("3000".equals(systemModule.getId())){
                    //根据一级菜单找到二级
                    List<SystemModuleVO> Children=new ArrayList<>();  //二级菜单集合
                    //二级菜单排序
                    List<SystemModuleVO> Childrens=Children.stream().sorted(Comparator.comparing(SystemModuleVO::getForderindex)).collect(Collectors.toList());
                    SystemModuleVO systemModuleVO = new SystemModuleVO();//菜单树对象
                    systemModuleVO.setId(systemModule.getId());
                    systemModuleVO.setLabel(systemModule.getFmainalias());
                    systemModuleVO.setPath(systemModule.getWPath());
                    SystemModuleVO ChildrensModule = new SystemModuleVO();
                    ChildrensModule.setId("1006");
                    ChildrensModule.setLabel("个人签到");
                    ChildrensModule.setPath("/mySign/index");
                    Childrens.add(ChildrensModule);
                    systemModuleVO.setChildren(Childrens);
                    //添加到集合
                    systemModuleVOList.add(systemModuleVO);
                }
            }
        }

        return systemModuleVOList;
    }
    @Override
    public List<CustomCodeListDTO> getCustomCodes() {
        CustomCodeListDTO customCodeListDTO=new CustomCodeListDTO();
        customCodeListDTO.setCustomCode("b017dbfe-e9a5-4ba2-86e0-fb543372c882");
        customCodeListDTO.setCustomName("广州分署");
//        customCodeListDTO.setCustomCode("b017dbfe-e9a5-4ba2-86e0-fb543372c882");
//        customCodeListDTO.setCustomName("重庆海关");
        CustomCodeListDTO customCodeListDTO1=new CustomCodeListDTO();
        customCodeListDTO1.setCustomCode("94480300-fca2-4268-86c2-4c13963bf4cb");
        customCodeListDTO1.setCustomName("海关总署");
        CustomCodeListDTO customCodeListDTO2=new CustomCodeListDTO();
        customCodeListDTO2.setCustomCode("bba02f17-b961-4f89-9b87-b2fbb0067268");
        customCodeListDTO2.setCustomName("杭州海关");
        return Arrays.asList(customCodeListDTO,customCodeListDTO1,customCodeListDTO2);
    }

    @Override
    public String setCustom( String customCode ) {
        return "设置成功";
    }

    private void  getChild(SystemModule systemModule, List<SystemModuleVO> Children , List<SystemModule> moduleList ){
        List<SystemModule> myChildren=moduleList.stream().filter(i->i.getFsuperid()!=null&&i.getFsuperid().toString().equals(systemModule.getId())).collect(Collectors.toList());
        List<SystemModuleVO> myChildrenVO=new ArrayList<>();
        for (SystemModule myChild : myChildren) {
            SystemModuleVO systemModuleVO = new SystemModuleVO();
            systemModuleVO.setId(myChild.getId());
            systemModuleVO.setLabel(myChild.getFmainalias());
            systemModuleVO.setPath(myChild.getWPath());
            systemModuleVO.setForderindex(myChild.getForderindex());
            myChildrenVO.add(systemModuleVO);
        }
        if (!CollectionUtils.isEmpty(myChildren)){
            Children.addAll(myChildrenVO);
            for (SystemModule child:myChildren){
                getChild(child,Children,moduleList);
            }
        }else {
            return ;
        }
    }
}
